#!/bin/bash

modname=""
libname=""
exename=""

while [[ "$#" -gt 0 ]]; do
    case $1 in
        -l | --libname) libname=$2; shift;;
        -m | --modname) modname=$2; shift;;
        -e | --exename) exename=$2; shift;;
        *) echo "unknow parameter passed: $1"; exit 1;;
    esac
    shift
done


if [ -z "$modname" ]; then
    echo "ERROR: invalid module name\nUsage:\n -m <module name> -l <library name> -e <exe name>";
fi

if [ -z "$libname" ]; then
    echo "ERROR: invalid library name\nUsage:\n -m <module name>-l <library name> -e <exe name>";
    
fi

if [ -z "$exename" ]; then
    echo "ERROR: executable name\nUsage:\n -m <module name> -l <library name> -e <exe name>";
fi

echo -e "add_subdirectory(./$modname)\n" >> CMakeLists.txt

mkdir "$modname"
cd "$modname"
mkdir headers
mkdir docs
touch CMakeLists.txt
touch impl.cpp
touch main.cpp

echo -e "set(libname $libname)\nset(exename $exename)\n"\
    "include_directories("'${CMAKE_CURRENT_SOURCE_DIR}'"/headers)\n"\
    "add_library("'${libname}'" SHARED impl.cpp)\n"\
    "add_executable("'${exename}'" main.cpp)\n"\
    "target_link_libraries("'${exename}'" PUBLIC "'${libname}'")"\
    >> CMakeLists.txt

